package com.xudow.app.ui.adapter.user;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.base.Activities;
import com.activeshare.edu.ucenter.models.user.UserFavoriteItemsWithOther;
import com.daimajia.swipe.SwipeLayout;
import com.xudow.app.R;

import java.text.SimpleDateFormat;
import java.util.List;

import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 我的收藏(活动) List Adapter
 *
 * @author lukecheng
 */
public class FavoriteActivityAdapter extends RecyclerView.Adapter {
    private static final String TAG = makeLogTag(FavoriteActivityAdapter.class);

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);

        void onDelete(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    /**
     * 收藏
     */
    private List<UserFavoriteItemsWithOther> favorites;

    public FavoriteActivityAdapter(List<UserFavoriteItemsWithOther> favorites) {
        this.favorites = favorites;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_favorite_activity_list, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        Activities activity = favorites.get(position).getActivities();
        viewHolder.title.setText(activity.getTitle());
        viewHolder.address.setText(activity.getActivityAddress());
        viewHolder.time.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(activity.getRegisterDueDate()));

        //TODO:没有简介
        //viewHolder.enrollCount.setText(activity.getQuota());

        //TODO:缺少活动图片
    }

    @Override
    public int getItemCount() {
        return favorites.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        private LinearLayout detailItem;
        public SwipeLayout root;
        public ImageView image;
        public TextView title;
        public TextView time;
        public TextView desc;
        public TextView address;

        public ViewHolder(View itemView) {
            super(itemView);

            detailItem = (LinearLayout) itemView.findViewById(R.id.detail_item);
            root = (SwipeLayout) itemView.findViewById(R.id.root);

            detailItem.setOnClickListener(this);
            detailItem.setOnLongClickListener(this);

            image = (ImageView) itemView.findViewById(R.id.activity_img);
            title = (TextView) itemView.findViewById(R.id.title);
            desc = (TextView) itemView.findViewById(R.id.desc);
            time = (TextView) itemView.findViewById(R.id.time);
            address = (TextView) itemView.findViewById(R.id.address);

            root.addSwipeListener(new SwipeLayout.SwipeListener() {
                @Override
                public void onStartOpen(SwipeLayout swipeLayout) {

                }

                @Override
                public void onOpen(SwipeLayout swipeLayout) {
                    LinearLayout bottomWrap = (LinearLayout) swipeLayout.findViewById(R.id.bottom_wrapper);
                    LinearLayout deleteItem = (LinearLayout) swipeLayout.findViewById(R.id.delete_item);
                    ImageView deleteImage = (ImageView) swipeLayout.findViewById(R.id.delete_image);
                    TextView deleteText = (TextView) swipeLayout.findViewById(R.id.delete_text);

                    View.OnClickListener listener = new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (null != onRecyclerViewListener) {
                                onRecyclerViewListener.onDelete(ViewHolder.this.getAdapterPosition());
                            }
                        }
                    };

                    bottomWrap.setOnClickListener(listener);
                    deleteItem.setOnClickListener(listener);
                    deleteImage.setOnClickListener(listener);
                    deleteText.setOnClickListener(listener);
                }

                @Override
                public void onStartClose(SwipeLayout swipeLayout) {

                }

                @Override
                public void onClose(SwipeLayout swipeLayout) {

                }

                @Override
                public void onUpdate(SwipeLayout swipeLayout, int i, int i1) {

                }

                @Override
                public void onHandRelease(SwipeLayout swipeLayout, float v, float v1) {

                }
            });
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                if (v.getId() == R.id.detail_item) {
                    onRecyclerViewListener.onItemClick(this.getAdapterPosition());
                }
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(this.getAdapterPosition());
            }

            return false;
        }
    }
}
